package edu.gdut.tiantian.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import edu.gdut.tiantian.entity.CartItem;


@Repository
public interface CartDao {
	
	@Insert("insert into cart(userId,productId,buyNum) values(#{userId},#{productId},#{buyNum})")
	void addProductToCart(@Param("userId")String userId,@Param("productId")String productId,@Param("buyNum")int buyNum);
	
	//清空购物车
	@Delete("delete from cart where userId=#{userId}")
	void clearCart(String userId);
	
	//删除购物项
	@Delete("delete from cart where cartItemId=#{cateItemId}")
	void delCartItem(int cartItemId);
	
	@Select("select * from cart where userId=#{userId}")
	@Results({
		@Result(property="product",column="productId",one = @One(select="edu.gdut.tiantian.dao.ProductDao.findById"))
	})
	List<CartItem> getCart(String userId);
	
	@Select("select * from cart where userId=#{userId} and productId=#{productId}")
	CartItem findById(@Param("userId")String userId,@Param("productId")String productId);
	
	@Update("update cart set buyNum=#{buyNum} where userId=#{userId} and productId=#{productId}")
	void updateCateItem(@Param("userId")String userId,@Param("productId")String productId,@Param("buyNum")int buyNum);
}
